package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        try (Connection conn = DBUtils.getConn()){
            String sql = "select id from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);

            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                System.out.println("用户名已存在!");
                return;
            }
            System.out.println("请输入密码");
            String password = sc.nextLine();
            System.out.println("请输入昵称");
            String nick = sc.nextLine();
            PreparedStatement regPs =
                    conn.prepareStatement(
                            "insert into user values(null,?,?,?)");
            regPs.setString(1,username);
            regPs.setString(2,password);
            regPs.setString(3,nick);
            regPs.executeUpdate();//执行插入SQL语句
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
